package com.wlyuan.open.domain.api.utils;

import com.alibaba.fastjson.JSON;
import com.wlyuan.open.domain.api.OpenApiInfo;
import com.wlyuan.open.domain.api.OpenApiInfoLogId;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 */
@Slf4j
public class MappingOpenApiInfoLog {

    public OpenApiInfoLogId create(Long value) {
        return Optional.ofNullable(value)
                .map(OpenApiInfoLogId::new)
                .orElse(null);
    }

    public Long value(OpenApiInfoLogId source) {
        return Optional.ofNullable(source).map(s -> s.getId()).orElse(null);
    }

    public String apisToString(List<OpenApiInfo> apis) {
        return Optional.ofNullable(apis)
                .map(JSON::toJSONString)
                .orElse(null);
    }

    public List<OpenApiInfo> stringToApis(String source) {
        return Optional.ofNullable(source)
                .map(string -> JSON.parseArray(string, OpenApiInfo.class))
                .orElse(null);
    }

}
